package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC791 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String customSortString(String order, String str) {
            int cnt[] = new int[26];
            for (char ch : str.toCharArray()) {
                cnt[ch - 'a']++;
            }

            StringBuilder res = new StringBuilder();
            for (char ch : order.toCharArray()) {
                for (int i = 0; i < cnt[ch - 'a']; i++) {
                    res.append(String.valueOf(ch));
                }
                cnt[ch - 'a'] = 0;
            }

            for (int i = 0; i < 26; i++) {
                for (int j = 0; j < cnt[i]; j++) {
                    res.append(String.valueOf((char) ('a' + i)));
                }
            }
            return res.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.customSortString("cba", "abcd"));
    }

    public static void main(String[] args) throws Exception {
        LC791 an = new LC791();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
